﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
//using Login.Models;
//using Login.Respostories;
//using Login.Utils;
using MD5Hash;
using WindowsFormsApp1;
using WindowsFormsApp1.Models;
using WindowsFormsApp1.Respostories;
using WindowsFormsApp1.Utils;

namespace Login
{
    public partial class LoginForm : Form
    {
        public LoginForm()
        {
            InitializeComponent();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            string mobile = txtMobile.Text;
            string password = txtPassword.Text;

            Regex mobilePattern = new Regex(@"^1[3-9][0-9]{9}$");

            //验证手机号
            if(string.IsNullOrEmpty(mobile) || !mobilePattern.IsMatch(mobile))
            {
                lblMobileErrorMsg.Visible = true;
                return;
            }
            lblMobileErrorMsg.Visible = false;

            //验证密码
            if (string.IsNullOrEmpty(password)|| password.Length <4 || password.Length > 12)
            {
                lblPasswordErrorMsg.Visible = true;
                return;
            }
            lblPasswordErrorMsg.Visible = false;

            //登录
            try
            {
                User user = UserRespostoriy.Login(mobile, password.GetMD5());
                if (user == null)
                {
                    MessageBox.Show("账号或密码错误", "登录提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                //保存用户登录信息
                GlobalData.AdminUser = user;

                //跳转到主界面
                MainForm mainForm = new MainForm();
                this.Hide(); //隐藏登录界面
                mainForm.Show();

            }
            catch (Exception ex)
            {

                MessageBox.Show("登录失败:" + ex.Message);
            }


        }
    }
}
